package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApReadBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.common.util.ArticleThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApReadBehaviorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-03-19
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements IApReadBehaviorService {
    @Autowired
    private ApReadBehaviorMapper apReadBehaviorMapper;
    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        //判断请求非空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 获取本地线程内user对象
        User user = AppThreadLocalUtil.get();
        //判断非空
        if (dto.getEquipmentId() != null&&user!=null) {
            //构建查询条件
            LambdaQueryWrapper<ApReadBehavior> query = new LambdaQueryWrapper<>();
            query.eq(ApReadBehavior::getEntryId, user.getUserId());
            query.eq(ApReadBehavior::getArticleId, dto.getArticleId());
            //判断文章阅读生成次数
            int count = this.count(query);
            Date date = new Date();
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            if (count == 1) {
                //设置唯一
                ApReadBehavior apReadBehavior1 = this.apReadBehaviorMapper.selectOne(query);
                apReadBehavior1.setCount(apReadBehavior1.getCount() + 1);
                apReadBehavior1.setReadDuration(apReadBehavior1.getReadDuration() + dto.getReadDuration());
                apReadBehavior1.setCreatedTime(date);
                apReadBehavior1.setLoadDuration(dto.getLoadDuration());
                apReadBehavior1.setPercentage(dto.getPercentage());
                Date date1 = new Date(System.currentTimeMillis() + dto.getReadDuration());
                apReadBehavior1.setUpdatedTime(date1);
                this.updateById(apReadBehavior1);
                return ResponseResult.okResult(apReadBehavior);
            }
            //未生成，则创建
            apReadBehavior.setReadDuration(dto.getReadDuration());
            apReadBehavior.setEntryId(user.getUserId());
            apReadBehavior.setCount(1);
            apReadBehavior.setArticleId(Long.valueOf(dto.getArticleId()));
            apReadBehavior.setLoadDuration(dto.getLoadDuration());
            apReadBehavior.setPercentage(dto.getPercentage());
            apReadBehavior.setCreatedTime(date);
            Date date1 = new Date(System.currentTimeMillis() + dto.getReadDuration());
            apReadBehavior.setUpdatedTime(date1);
            this.save(apReadBehavior);
            return ResponseResult.okResult(apReadBehavior);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }



    private IApBehaviorEntryService entryService;
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        EntryDto entryDto = new EntryDto();
        entryDto.setUserId(AppThreadLocalUtil.get().getUserId());
        entryDto.setEquipmentId(dto.getEquipmentId());
        ApBehaviorEntry entry = entryService.getEntry(entryDto);
        Integer entryId;
        entryId=(entry.getId());
        //找出对应的一个阅读行为
        LambdaQueryWrapper<ApReadBehavior> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApReadBehavior::getArticleId, dto.getArticleId()).eq(ApReadBehavior::getEntryId, entryId);
        ApReadBehavior readBehavior = getOne(queryWrapper);
        if(readBehavior==null) {
            readBehavior = new ApReadBehavior();
            //复制属性
            BeanUtils.copyProperties(dto, readBehavior);
        }
        else
        {   //累加属性
            readBehavior.setReadDuration(readBehavior.getReadDuration()+dto.getReadDuration());
            readBehavior.setPercentage(dto.getPercentage());
            readBehavior.setLoadDuration(readBehavior.getLoadDuration()+dto.getLoadDuration());
            readBehavior.setCount(readBehavior.getCount()+dto.getCount());
        }
        //判断是否新建并复制属性
        if (readBehavior.getId() == null) {
            readBehavior.setEntryId(entryId);
            readBehavior.setCreatedTime(new Date());
        }
        readBehavior.setUpdatedTime(new Date());
        saveOrUpdate(readBehavior);
        return ResponseResult.okResult();


    }
}
